package fr.esiag.isidis.fi.pdsTeam.generator;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;



import fr.esiag.isidis.fi.pdsTeam.UDP.IpPort;
import fr.esiag.isidis.fi.pdsTeam.UDP.RawInformationSender;
import fr.esiag.isidis.fi.pdsTeam.events.MyEvent;
import fr.esiag.isidis.fi.pdsTeam.parser.EventsFileParser;
import fr.esiag.isidis.fi.pdsTeam.tablesData.InformationTableModel;
import fr.esiag.isidis.fi.pdsTeam.tram.TramGenerator;

public class EventsGenerator extends Thread{
	
	static String xmlFileURL = "f:\\scenario.xml";
	
	static ArrayList<MyEvent> events;
	
	private EventsFileParser myParser;
	//private DataBaseAccess dbAccess;
	private RawInformationSender sender;
	private byte[] tram;
	
	private boolean stressMode = false;
	
	private InformationTableModel informations = new InformationTableModel();
	
	private IpPort ipPort = new IpPort("localhost", 11111);
	
	public IpPort getIpPort() {
		return ipPort;
	}

	public void setIpPort(IpPort ipPort) {
		this.ipPort = ipPort;
	}
	
	public void setIpPort(String ip, int port) {
		ipPort.setIp(ip);
		ipPort.setPort(port);
	}

	public InformationTableModel getInformtions() {
		return informations;
	}
	
	public void setInformations(InformationTableModel informations) {
		this.informations = informations;
	}



	private boolean play;
	private int position = 0;
	
	public boolean isPlay() {
		return play;
	}

	public void setPlay(boolean play) {
		this.play = play;
	}
	
	public boolean isStressMode() {
		return stressMode;
	}

	public void setStressMode(boolean stressMode) {
		this.stressMode = stressMode;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}
	
	public int getTotal() {
		return events.size();
	}

	public EventsGenerator() {
		events = new ArrayList<MyEvent>();
	}
	
	public boolean loadEventsfromXMLFile(String url){
		myParser = EventsFileParser.getInstance();
		events = myParser.getEventsFromXMLFile(url);
		return true;
	}
	
	/*public int buildEvents(){
		dbAccess = DataBaseAccess.getInstance();
		dbAccess.openConnexion();
		for (int i = 0; i <events.size(); i++) {
			
			events.get(i).setSensorType(dbAccess.getSensorType(events.get(i).getSensorMacAddress()));
			
			events.get(i).setSensorName(dbAccess.getSensorName(events.get(i).getSensorMacAddress()));
			
			events.get(i).setStation(dbAccess.getSensorStation(events.get(i).getSensorMacAddress()));
			
			events.get(i).setEventMessage(dbAccess.getEventMessage(events.get(i).getEventCode()));
			
		}
		dbAccess.closeConnexion();
		return 1;
	}*/
	
	@SuppressWarnings("deprecation")
	public void startEventSending() throws UnknownHostException, InterruptedException{
		if(!stressMode){
			while (true) {
				if(position<events.size()){
					if(play){
						tram = TramGenerator.generateTram(events.get(position).getSensorMacAddress(), events.get(position).getEventCode());
						
						sender = RawInformationSender.getInstance();
						sender.setIpPort(ipPort);
						Thread.sleep(events.get(position).getWaitTime());
						sender.sendEventTram(tram);
						informations.addInformation(events.get(position).getSensorMacAddress(), events.get(position).getEventCode(), (new Date()).toGMTString(), events.get(position).getWaitTime());
			            
						position ++;
					}
				}else{
					// preparer la relance de la simulation
					play = false;
					position = 0;
					informations.resetNum();
				}
			}
		}else {
			while (true) {
				if(position<events.size()){
					if(play){
						tram = TramGenerator.generateTram(events.get(position).getSensorMacAddress(), events.get(position).getEventCode());
						
						sender = RawInformationSender.getInstance();
						Thread.sleep(events.get(position).getWaitTime());
						sender.sendEventTram(tram);
						position ++;
					}
				}else{
					play = false;
					position = 0;
					informations.resetNum();
				}
			}
		}
	}
	
	@Override
	public void run() {
		super.run();
		try {
			startEventSending();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	
	
	/*public static void main(String[] args) {
		
		new EventsGenerator();
		
		dbAccess.openConnexion();
		
		for (int i = 0; i <events.size(); i++) {
			
			events.get(i).setCapteurType(dbAccess.getCapteurType(events.get(i).getCapteurMacAddress()));
			
			events.get(i).setCapteurName(dbAccess.getCapteurName(events.get(i).getCapteurMacAddress()));
			
			events.get(i).setStation(dbAccess.getCapteurStation(events.get(i).getCapteurMacAddress()));
			
			events.get(i).setEventMessage(dbAccess.getEventMessage(events.get(i).getEventCode()));
			
		}
		
		dbAccess.closeConnexion();
		
	}*/

}
